
//获取金币节点
var count_money = document.querySelector('.count');
var count=0
//获取显示+金币
var addMoney = document.querySelector('.addMoney');


// 计时
var showTime = 8
var time_content = document.querySelector('.time_content')

//获取开始按钮，点击隐藏并开始
//获取蒙板元素
var btn_begin = document.querySelector('.begin_btn')
var begin = document.querySelector('.begin')
//获取结束蒙板
var end = document.querySelector('.end')
// 总金额蒙版
var end_count = document.querySelector('.end_count')

//红包数组
var redPackages = []
btn_begin.addEventListener("click",function(e){
    begin.style.display = "none"

    var newPackage = new RedPackage();
    redPackages.push(newPackage)
    setInterval(function(){
        let newPackage = new RedPackage();
        redPackages.push(newPackage)
    },800)
    setInterval(function() {
        ctx.clearRect(0, 0, window.innerWidth, window.innerHeight)
        for(let i = 0;i<redPackages.length; i++){
            redPackages[i].drawRed()
            redPackages[i].move()
        }        
    }, 6)




    // 倒计时
    console.log(time_content)
    var time1 = setInterval(function(){
        showTime --
        time_content.innerHTML = showTime
        if(showTime === 0){
            //计时结束弹出蒙板
            clearInterval(time1)
            end_count.innerHTML = count
            end.style.display = "block"
        }
    },1000)

});


            // 删除第i个
canvas.onclick = function(e){
    
    for(let i = 0;i<redPackages.length; i++){
            // console.log('这是图片y轴的位置'+redPackages[i].img_top)
            // console.log(e.clientY)
        //判断鼠标点击的位置是否在红包范围内
        if(e.clientY > redPackages[i].img_top && e.clientY < redPackages[i].img_top+100 && e.clientX> redPackages[i].img_left  && e.clientX < redPackages[i].img_left+100){
            // console.log("yes")
            // console.log("鼠标位置Y:"+e.clientY);
            // console.log('这是第几个'+i)

            redPackages.splice(i,1)
            //随机加金币，并赋值
            var money = Math.ceil(Math.random()*10)
            count+=money
            count_money.innerHTML = count
            addMoney.innerHTML= '+'+money

            //控制opacity  显示淡入淡出
            addMoney.style.opacity=1
            var hidden = setTimeout(() => {
                addMoney.style.opacity=0
                clearInterval(hidden)
            }, 1000);
        }
    }
}


